var express = require('express');
var user = require('../model/user')
var router = express.Router();
//md5加密
var utils = require('utility');
var chat = require('../model/chat')
const _filter = {'password':0,'__v':0}
router.post('/register',function (req,res) {
    //console.log(req.body);
    let {username,password,type} = req.body;
    var admin = new user({
        user:username,
        password:mymd5(password),
        type:type,
    })
    admin.save((err,ret)=>{
        if(err){
            console.log('保存失败');
            return res.json({code:1,message:'添加失败'});
        }
        else{
            //res.cookie('userid',re)
            //console.log(ret);
            res.cookie('userid',ret._id);
            return res.json({code:0,message:'添加成功',data:ret});
        }
    });
});
router.post('/login',(req,res)=>{
    let {username,password} = req.body;
    //let {userid} = req.cookies;
    //console.log(userid+'123');
    user.findOne({user:username,password:mymd5(password)},_filter,(err,data)=>{
        if(err){
            //console.log('保存失败');
            return res.json({code:1,message:'查询失败'});
        }else{
            //console.log(data);
            if(data==null){
                return res.json({code:2,message:'改账号未注册'});
            }else{
                //console.log(data._id);
                //设置cookie
                res.cookie('userid',data._id);
                return res.json({code:0,message:'登录成功',data:data});

            }
            
        }
    });
});
router.get('/info',function(req, res){
    const {userid} = req.cookies
    console.log(userid);
	if (!userid) {
		return res.json({code:1})
	}
	user.findOne({_id:userid} ,_filter , function(err,doc){
		if (err) {
			return res.json({code:1, msg:'后端出错了'});
		}
		if (doc){
			return res.json({code:0,data:doc});
		}
	})
	//用户有没有cookie
	
})
router.post('/info',function(req, res){
    
    let type = req.body.type;
    console.log(type);
	user.find({type:type} , function(err,doc){
		if (err) {
			return res.json({code:1, msg:'后端出错了'});
		}
		if (doc){
			return res.json({code:0,data:doc});
		}
	})
	//用户有没有cookie
	
})
router.post('/updateBossInfo',(req,res)=>{
    const {userid} = req.cookies;
    if (!userid) {
		return res.json({code:1})
    }
    let body = req.body;
    user.findByIdAndUpdate(userid,body,(err,data)=>{
        if(err){
            return res.json({code:1, msg:'后端出错了'});
        }else{
            //console.log(data);
            return res.json({code:0,data:data})
        }
    })
})
router.post('/updateGeniusInfo',(req,res)=>{
    const {userid} = req.cookies;
    if (!userid) {
		return res.json({code:1})
    }
    let body = req.body;
    user.findByIdAndUpdate(userid,body,(err,data)=>{
        if(err){
            return res.json({code:1, msg:'后端出错了'});
        }else{
            //console.log(data);
            return res.json({code:0,data:data})
        }
    })
})
router.post('/getGeniusInfo',(req,res)=>{
    const {userid} = req.cookies;
    if (!userid) {
		return res.json({code:1})
    }
    let type = req.body.type;
    user.find({type:type},_filter,(err,data)=>{
        if(err){
            return res.json({code:1, msg:'后端出错了'})
        }else{
            //console.log(data);
            return res.json({code:0,data:data})
        }
    })
});
router.get('/getChatList',(req,res)=>{
    const {userid} = req.cookies;
    chat.find((err,data)=>{
        if(err){
            return res.json({code:1, msg:'后端出错了'})
        }else{
            return res.json({code:0,data:data,userid:userid});
        }
    })
})
router.post('/getAvatar',(req,res)=>{
    var _id = req.body._id;
    user.findOne({_id:_id},_filter,(err,data)=>{
        if(err){
            return res.json({code:1, msg:'后端出错了'});
        }else{
            if(data == null){
                return res.json({code:1,msg:'未查找到数据'});
            }else{
                //console.log(data)
                return res.json({code:0,data:data});
            }
        }
    })
})
router.post('/infos',function(req, res){
    const {userid} = req.cookies
    console.log(userid);
	if (!userid) {
		return res.json({code:1})
	}
	user.findOne({_id:userid}, function(err,doc){
        console.log(doc);
		if (err) {
			return res.json({code:1, msg:'后端出错了'});
		}
		if (doc){
            //return res.json({code:0,data:doc});
            var _id = req.body._id;
            console.log(_id)
            user.findOne({_id:_id},(err,data)=>{
                
                        if(err){
                            return res.json({code:1, msg:'后端出错了'});
                        }else{
                            if(data == null){
                                return res.json({code:1,msg:'未查找到数据'});
                            }else{
                                //console.log(data)
                                return res.json({code:0,data:doc,otherAvatar:data.avatar});
                            }
                        }
                    })
		}
	})
	//用户有没有cookie
	
})
router.get('/getAllUser',(req,res)=>{
    user.find((err,data)=>{
        //console.log(data);
        if(err){
            return res.json({code:1, msg:'后端出错了'});
        }else{
            if(data==null){
                return res.json({code:1,msg:'未查找到数据'});
            }else{
                return res.json({code:0,data:data});
            }
        }
    })
})
function mymd5(pwd){
    var str = 'ywj_is_handsome_%&&8**(())***&hsh8jcnmnjn%%8^^~~~```&^78'
    let salt = utils.md5(utils.md5(pwd+str));
    return salt;
}

module.exports = router;